User Post
[Dev] fmt.Println, fmt.Fprintf, fmt.Sprintf in Go Language (Golang)
Last edited by dave on 03/02/2026, 14:48:43 UTC
User:dave / [Dev] fmt.Println, fmt.Fprintf, fmt.Sprintf in Go Language (Golang)
Contents
When should I use it?
Question 1: “Am I just printing something for a human?”
YES → use fmt.Println
fmt.Println("Hi,", name, age)
Use this for:
- debugging
- CLI output
- quick logs
- learning
Rule: if you don’t care about exact formatting → Println.
JS equivalent:
console.log("Hi", name, age)
Question 2: “Do I need a formatted string or to write somewhere specific?”
YES → use fmt.Fprintf or fmt.Sprintf
A) Writing somewhere (HTTP, file, buffer)
➡ fmt.Fprintf
fmt.Fprintf(w, "Hi %s, age %d\n", name, age)
Think:
“formatted print to something”
JS equivalent:
res.write(`Hi ${name}, age ${age}`)
B) Need the string value
➡ fmt.Sprintf
msg := fmt.Sprintf("Hi %s, age %d", name, age)
Think:
“string printf”
JS equivalent:
const msg = `Hi ${name}, age ${age}`
TLDR
| What you want | Use | Newline (\n) |
|---|---|---|
| Just print | fmt.Println | Yes |
| Print formatted | fmt.Printf | No |
| Write formatted to HTTP/file | fmt.Fprintf | No |
| Build a formatted string | fmt.Sprintf | No |
You can survive your entire Go job knowing only:
PrintlnSprintfFprintf
Final mental hack (remember this)
P = Print
F = Format
S = String
- Println → prints
- Sprintf → string
- Fprintf → formatted output
Backlinks (0)
- General
No backlinks yet.
- User
No backlinks yet.
- Redirects
No backlinks yet.
- Media
No backlinks yet.
- Categories
No backlinks yet.
Categories (0)
No categories assigned to this page.
Edit Level
> User's own page (User:dave and admins only)
Latest on Lounge
Join the conversation about the 'User:dave/[Dev] fmt.Println, fmt.Fprintf, fmt.Sprintf in Go Language (Golang)' article →
No comments yet. Be the first to comment!
Text content on User: pages and its subpages is not licensed under the same terms as regular wiki pages. All rights reserved.